<template>
  <div class="userslist">
    <div class="btnCls">
      <el-button size="mini" @click="getUserList">{{ $t('btn.reload') }}</el-button>
      <!--      :disabled="bomCost('per:insertAdmin')"-->
      <el-button type="primary" size="mini" @click="newly">{{ $t('btn.add') }}</el-button>
      <el-dropdown class="marL" size="mini" trigger="hover">
        <el-button type="warning" size="mini">{{ $t('btn.config') }}
          <i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="forbidden(1)">{{ $t('btn.disab') }}<!--禁用--></el-dropdown-item>
          <el-dropdown-item @click.native="forbidden(2)">{{ $t('btn.dis') }}<!--反禁用--></el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown class="marL" size="mini" trigger="hover">
        <el-button type="primary" size="mini">{{ $t('nav.password') }}<!--密码策略-->
          <i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="resetVisable=true">{{ $t('nav.resetPas') }}<!--重置密码--></el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="searchMaterialList"
        />
        <el-button size="mini" type="primary" class="btn" @click="searchMaterialList">
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="searchMaterialList"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        table-selection
        sortable
        get-row-keys="fuserId"
        @selectionChange="selectionDatas"
        @clickRow="userDetails"
      >
        <template v-slot:btnState="col">
          <el-link type="primary" @click="userDetails(col.scope.row)">{{ col.scope.row.fname }}</el-link>
        </template>
        <template v-slot:tagSlot="col">
          <el-tag v-if="col.scope.row.fforbidStatus === 'A'" type="primary">{{ $t('market.normal') }}</el-tag>
          <el-tag v-else type="danger">{{ $t('btn.disab') }}<!--禁用--></el-tag>
        </template>
      </jc-table>
      <cardTab :tab-arr="tabArr" field="fprimaryGroupName" @search="handleQueryBomList" />
    </div>
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="pageNum"
        :limit.sync="size"
        @pagination="getUserList"
      />
    </div>
    <el-dialog
      v-dialogDrag
      :title="$t('nav.resetPas')"
      :visible.sync="resetVisable"
      width="500px"
      top="5vh"
      center
      :close-on-click-modal="false"
      close-on-press-escape
    >
      <el-form :model="form">
        <el-form-item :label="$t('nav.passwordnew')">
          <el-input v-model="form.fpassword" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetVisable = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="resetPas">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  userslist,
  resetUserPassword,
  disable,
  unDisable
} from '@/api/basicManagement/userslist'
import { userDisable } from '@/components/ToMxamineState'
import search from '@/components/Search/index'
import searData from '@/components/Search/mixin'
import filtration from '@/components/filtration'
export default {
  name: 'Index',
  components: {
    cardTab: () => import('@/components/cardTab'),
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 26,
      fbillNo: 'fname',
      total: 0, // 总条目
      pageNum: 1, // 当前页
      size: 200, // 每页显示多少条数据
      resetVisable: false,
      form: {
        fpassword: ''
      },
      tabArr: [
        { label: '内销', name: '内销' },
        { label: '外销', name: '外销' },
        { label: '信息', name: '信息' },
        { label: '财务', name: '财务' },
        { label: '供应链', name: '供应链' },
        { label: '采购', name: '采购' },
        { label: '营销', name: '营销' },
        { label: '研发', name: '研发' },
        { label: '仓库', name: '仓库' }
      ],
      tableData: [], // 列表数据
      // 表头数据
      tableHeader: [
        { label: this.$t('market.userGroup'), prop: 'fprimaryGroupName' },
        { label: this.$t('market.client'), type: 'state', prop: 'fname' },
        { label: this.$t('market.explain'), prop: 'fdescripTion' },
        { label: this.$t('market.sex'), prop: 'fsex' },
        { label: this.$t('market.account'), prop: 'fuserAccount' },
        { label: this.$t('market.phones'), prop: 'fphone' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate' },
        { label: this.$t('market.forbidStatu'), type: 'tag', prop: 'fforbidStatus' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.getUserList()
    this.tableHeader1 = this.tableHeader
  },
  methods: {
    // 用户列表
    async getUserList(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        formId: 'sec_user',
        pageNum: this.pageNum,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: res } = await userslist(DATA)
      this.tableData = res.array.map(item => {
        return userDisable(item)
      })
      this.total = res.total
      this.loading = false
    },
    // 详情
    userDetails(item) {
      const id = item.fuserId
      this.$router.push({ path: `/editUser/${id}` })
    },
    // 搜索物料
    searchMaterialList(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.getUserList(ev.detail)
      } else {
        this.getUserList()
      }
    },
    // 筛选
    handleQueryBomList(ev) {
      this.searCollData = []
      this.pageNum = 1
      this.searCollData = ev
      this.searchSon = ev
      this.getUserList()
    },
    // 选中
    selectionDatas(val) {
      this.val = val
      this.selectFbi = []
      val.forEach(res => {
        this.selectFbi.push(res.fuserId + '')
      })
    },
    // 禁用
    forbidden(ev) {
      const DATA = {
        Numbers: this.selectFbi
      }
      const api = ev === 1 ? disable : unDisable
      api(DATA).then(res => {
        if (res.code === 0) {
          this.getUserList()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 重置密码
    resetPas() {
      if (!this.val || this.val.length === 0 || this.val.length > 1) {
        this.$confirm('请选择一个用户', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      const DATA = {
        fpassword: this.form.fpassword,
        updateUserId: this.val[0].fuserId
      }
      resetUserPassword(DATA).then(res => {
        if (res.code === 0) {
          this.resetVisable = false
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    },
    newly() {
      this.$router.push({ path: '/newlyUser' })
    }
  }
}
</script>

<style scoped lang="scss">
.userslist {
  @include listBom;
  .marL {
    margin-left: 10px;
    cursor: default;
  }
  .table-content {
    margin-top: 5px;
  }
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
